package com.housekeeping.housekeeping.usermanagement.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBManager {
    private static String PROPERTIES_NAME = "jdbc.properties";
    private static DataSource dataSource;

    static {
        try {
            Properties properties = new Properties();
            properties.load(com.housekeeping.housekeeping.utils.DBManager.class.getClassLoader().getResourceAsStream(PROPERTIES_NAME));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("[连接池]\t获取连接池失败");
        } finally {
        }
    }

    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    //关流
    public static void close(AutoCloseable... closeables) {
        try {
            for (AutoCloseable closeable : closeables) {
                if (closeable != null) {
                    closeable.close();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }
}
